<?php 
include("globalModel.php");
class MY_Controller extends CI_Controller{ 
	
	var $DATA;
	var $upload_path	= './uploaded/';
	var $upload_allowed_types	= 'gif|jpg|png';
	var $upload_types	= 'image';	// image or file.
	var $upload_resize  = array(
				array('name'	=> 'thumb','width'	=> 70, 'height'	=> 70, 'quality'	=> '100%'),
				array('name'	=> 'medium','width'	=> 200, 'height'	=> 200, 'quality'	=> '100%')
			);
	var $message = "";
	var $folder_view = "";
	var $prefix_view = "";
	
	function __construct(){  
 
		parent::__construct(); 		  
		
		$this->_initConfig();
		$this->output->enable_profiler(false);
		$this->DATA = new globalModel();		
		$set=ini_set('mysql.trace_mode','Off');
   		ini_set('mysql.trace_mode',$set); 
		
		$this->lang->load('core', $this->jCfg['lang']);
		//$this->lang->load(strtolower($this->_getClass()), $this->jCfg['lang']);
		$this->load->helper('language');
	}
	
	function _initConfig(){
		$s = $this->session->userdata("jcfg");
		if(is_array($s))
			$this->jCfg = $s;
		else
			$this->_initSession();			
	}
	
	function isLogin(){
		if($this->jCfg['is_login'] == 1){
			redirect('');
		}else{
			redirect('auth');
		}
	}
	
	function current_session($i=0){
		for($x=0;$x>=$i;$x++){
			$this->_initSession();
		}
	}
	
	function _initSession(){
		$this->jCfg = array(
			'user' => array(
				'id' 		=> '',
				'name'		=> 'guest',
				'fullname'	=> 'Guest',
				'level'		=> '',
				'foto'		=> '',
				'emp_id'	=> '',
				'asuransi'	=> '',
				'cabang'	=> '',
				'access'	=> array()
			),
			'app_id'		=> '1',
			'app_name'		=> 'Integrated Office System',
			'complete_data'	=> 'yes',
			'is_login'		=> 0,
			'menu'			=> array(),
			'isearch'		=> array(
				'start_date'=> '',
				'end_date'	=> '',
				'status'	=> 'all',
				'per_page'	=> '5',
			),
			'trans'			=> array(),
			'mod_rewrite'	=> 1,
			'theme'			=> '',
			'referer'		=> '',
			'mod_design'	=> '',
			'access'		=> array(),
			'lang'			=> 'ind',
			'captcha'		=> array()			
		);
		$this->_releaseSession();
	}
	
	function setReferer($url=""){
		if($url==""){
			$this->load->library('user_agent');
			$url= $this->agent->referrer();
		}
		$this->jCfg['referer'] = $url;
		$this->_releaseSession();
	}
	
	function setMenu($menu=array()){
		$this->jCfg['menu'] = $menu;
		$this->_releaseSession();
	}
	
	function setiSearch($s=array()){
		$this->jCfg['isearch'] = $s;
		$this->_releaseSession();
	}
	
	function setLang($lang="en"){
		$this->jCfg['lang'] = $lang;
		$this->_releaseSession();
	}
	function setAccess($acc=array()){
		$this->jCfg['access'] = $acc;
		$this->_releaseSession();
	}
	
	function getReferer(){
		return $this->jCfg['referer'];
	}
	
	function _releaseSession(){
		$this->session->set_userdata(array("jcfg"=>$this->jCfg));
	}
	
	/*
	* stuff function
	*/
	function _getClass(){ // return name of current class
		return $this->router->fetch_class();
	}
	
	function _getMethod(){ // return name of current methode
		return $this->router->fetch_method();
	}
	
	function generateCaptcha($p=array()){
		$this->load->plugin('captcha');
		$this->load->helper('form');
		$captcha_vals = array(
								'word'      => '',
								'img_path'	=> './images/captcha/',
								'img_url'	=> base_url().'images/captcha/',
								'font_path'	=> './system/fonts/verdana.ttf',
								'img_width' => isset($p['width'])?$p['width']:165,
								'img_height'=> isset($p['height'])?$p['height']:45,
								'expiration'=> 50,
								'align'=>'absmiddle'
							 );
		
		$captcha = create_captcha($captcha_vals);
		
		$sesCaptcha = array(
								'captcha_time'	=> $captcha['time'],
								'ip_address'	=> $this->input->ip_address(),
								'captcha_word'	=> $captcha['word']
							);
							
		$this->jCfg['captcha']=$sesCaptcha;
		$this->_releaseSession();
		
		return $captcha['image'];					 
	}
	
	function cekCaptcha($word=''){
		$s = false;
		if(trim($word)!=''){
			if(strtoupper($word)===strtoupper($this->jCfg['captcha']['captcha_word'])){
				return true;
			}
		}
		return $s;
	}
	
	function sendEmail($p=array()){
		$this->load->library('email');
		
		/*mail method */
		$config['protocol'] = 'mail';
		$config['mailtype'] = isset($p['type'])?$p['type']:'html';
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		
		/*smtp method */
		/*$config['protocol']  = 'mail';
		$config['smtp_host'] = 'smtp.gmail.com';
		$config['smtp_port'] = 465;
		$config['smtp_user'] = 'hhumaedi@gmail.com';
		$config['smtp_pass'] = '*****';
		$config['priority']  = 1;
		$config['mailtype']  = 'html';
		$config['charset']   = 'utf-8';
		$config['wordwrap']  = TRUE;*/
		
		/* send mail method */
		/*$config['protocol'] = 'sendmail';
		$config['mailtype'] = 'html';
		$config['charset']  = 'utf-8';
		$config['wordwrap'] = TRUE;
		*/
		$this->email->initialize($config);

		$this->email->from($p['from'], isset($p['title'])?$p['title']:'AA Pialang ');
		$this->email->to($p['to']);			
		$this->email->subject('AA Pialang :'.$p['subject']);
		$this->email->to($p['bcc']);	
		$this->email->message($p['message']);
		
		if(isset($p['alt_message']) && trim($p['alt_message'])!='' ){
			$this->email->set_alt_message($p['alt_message']);
		}
		
		$this->email->send();
	}

	function _uploaded($par=array()){
		$this->load->library('image_lib');
		
		$uri = $this->upload_path;		
		if($_FILES[$par['input']]['error']==4)
			return false;
		$uId = uniqid();
		$fileName = $uId;
		$config['upload_path'] = $uri."original/";
		$config['file_name'] = $fileName;
		$config['allowed_types'] = $this->upload_allowed_types;
		$config['max_size']		= 1024*10;
		if(trim($this->upload_types)=='image'){
			$config['max_width']  	= 1024*2;
			$config['max_height'] 	= 768*2;
		}
		$this->load->library('upload', $config);
	
		if( $this->upload->do_upload($par['input']) )
		{	
			$img = $uId.$this->upload->file_ext;
			
			$this->_delte_old_files($par['param']);
			$this->DATA->_update($par['param']['par'],array($par['param']['field']=>$img));
			
			if(trim($this->upload_types)=='image'){ // if type of file is image
			
				$fileNameResize = $config['upload_path'].$fileName.$this->upload->file_ext;
				$img = getimagesize($fileNameResize);
				$realWidth	= $img[0];
				$realHeight = $img[1];
				
				$resize = array();
				foreach($this->upload_resize as $r){
					$resize[] = array(
						"width"			=> $r['width'],
						"height"		=> $r['height'],
						"quality"		=> $r['quality'],
						"source_image"	=> $fileNameResize,
						"new_image"		=> $uri.$r['name']."/".$fileName.$this->upload->file_ext
					);
				}
				
				foreach($resize as $k=>$v){
					$oriW = $v['width'];
					$oriH = $v['height'];
					$x = $v['width']/$realWidth;
					$y = $v['height']/$realHeight;
					if($x < $y) {
						$v['width'] = round($realWidth*($v['height']/$realHeight));
					} else {
						$v['height'] = round($realHeight*($v['width']/$realWidth));
					}     
				
					$this->image_lib->initialize($v); 
					if(!$this->image_lib->resize()){
						die("Error Resize....");
					}
					$this->image_lib->clear();
					if($k==0){
						$config = null;
						$config['image_library'] = 'GD2';
						$im = getimagesize($v['new_image']);
						$toCropLeft = ($im[0] - ($oriW *1))/2;
						$toCropTop = ($im[1] - ($oriH*1))/2;
						
						$config['source_image'] = $v['new_image'];
						$config['width'] = $oriW;
						$config['height'] = $oriH;
						$config['x_axis'] = $toCropLeft;
						$config['y_axis'] = $toCropTop;
						$config['maintain_ratio'] = false;
						
						$this->image_lib->initialize($config);
						 
						if(!$this->image_lib->crop()){
							die("Error Crop..");
						}
						$this->image_lib->clear();
					}
				}
			} // end if this type image
			
		}
		else {
			die("error uploaded...");
		}
	}

	function _delte_old_files($par=array()){
		$uri = $this->upload_path;
		$files = $this->DATA->data_id($par['par']);
		if( !empty( $files->{$par['field']} ) ){
			$ori_file = $uri.'original/'.$files->{$par['field']};
			if(file_exists($ori_file)){
				unlink($ori_file);
			}
			if(trim($this->upload_types)=='image' && count($this->upload_resize) > 0){				
				$data = array();
				foreach($this->upload_resize as $m){
					$data[] = $uri.$m['name']."/".$files->{$par['field']};
				}	
				foreach($data as $v){
					if(file_exists($v)){
						unlink($v);
					}
				}
			}				
		}
	}	

	
}